package com.china08.yxyapi.api.manage.tec.teaching.homework.workstatis;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.teaching.homework.workstatis1.WorkStatisticsQues5V1Service;

/**
 * 班级内学生完成情况(阅读类统计)
 */
@CrossOrigin
@RestController
public class WorkStatisticsQues5V1Controller extends YxyApiBaseController {

    @Autowired
    WorkStatisticsQues5V1Service workkStatisticsQues5V1Service;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/workstatis/ques5/statistics.v1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String classId, //
            @RequestParam Integer type, //
            @RequestParam String subject, //
            @RequestParam String stage, //
            ReqModel reqModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try { 
        	
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        reqModel.type = type;
        reqModel.classId = classId;
        reqModel.subject = subject;
        // return
        List<RespModel> respModels = new ArrayList<RespModel>();
        // 数据展示
        respModels = workkStatisticsQues5V1Service.statistics(classId, type, subject, stage);
        // 进行排序，先按照正确率排序，正确率一样就按照完成总数排序
        Collections.sort(respModels, new Comparator<RespModel>() {
            @Override
            public int compare(RespModel o1, RespModel o2) {
                int i = o2.doneTotal - o1.doneTotal;
                if (i == 0) { return (int) ((o2.duration * 100) - (o1.duration * 100)); }
                return i;
            }
        });

        return respModels;
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 班级Id
        public String classId;
        // type 类型 1-每天， 2-每周， 3-每月
        public Integer type;
        // 学段
        public String stage;
        // 学科Id
        public String subject;
        // 用户Id (不用传参数)
        public String userId;
        // 学校Id (不用传参数)
        public String schoolId;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // 学生Id
        public String studentId;
        // 学生名称
        public String studentNick;
        // 学生头像
        public String avatar;
        // 答题时长
        public long duration;
        // 总数
        public int total;
        // 已完成
        public int doneTotal;
        // 未完成
        public int noDoneTotal;
    }
}
